package xyz.zichengdulin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.zichengdulin.mapper.StudentMapper;
import xyz.zichengdulin.pojo.PageResult;
import xyz.zichengdulin.pojo.Student;
import xyz.zichengdulin.pojo.StudentQueryParam;
import xyz.zichengdulin.service.StudentService;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 学员条件分页查询
     *
     * @param studentQueryParam
     * @return
     */
    @Override
    public PageResult<Student> page(StudentQueryParam studentQueryParam) {
        //1.设置分页参数
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());

        //2.执行查询
        List<Student> studentList = studentMapper.page(studentQueryParam);

        //3.解析查询结果，并封装
        Page<Student> p = (Page<Student>) studentList;
        return new PageResult<Student>(p.getTotal(), p.getResult());

    }

    /**
     * 根据ids批量删除学员
     *
     * @param ids
     */
    @Override
    public void deleteByIds(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }

    /**
     * 添加学员信息
     *
     * @param student
     */
    @Override
    public void insert(Student student) {
        if (student.getCreateTime() == null) {
            student.setCreateTime(LocalDateTime.now());
            student.setUpdateTime(LocalDateTime.now());
        }
        studentMapper.insert(student);
    }

    /**
     * 根据id查询学生信息
     *
     * @return
     */
    @Override
    public Student getInfoById(Integer id) {
        return studentMapper.getInfoById(id);
    }

    /**
     * 更新学员信息
     *
     * @param student
     */
    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    /**
     * 学生违纪信息处理
     *
     * @param id
     * @param score
     */
    @Override
    public void violate(Integer id, Integer score) {
        studentMapper.violate(id, score);
    }
}
